#include<bits/stdc++.h>

using namespace std;
const int N=1e5+10;
int n,ans;
vector<int>s[N];
map<string,int>lqq;
int a[N];
vector<int>l;
bool vis[N];
void dfs(int x,int step){
	if(!s[x].size()){
		//cout<<step<<endl;
		vector<int>::iterator it=find(l.begin(),l.end(),1);
		if(it!=l.end()&&step>ans){
			ans=step;
		}
		return;
	}
	for(auto &j:s[x]){
		if(!vis[j]){
			vis[j]=true;
			l.push_back(j);
			cout<<j<<endl;
			dfs(j,step+1);
			vis[j]=false;
			l.pop_back();
		}
	}
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin>>n;
	string s1;
	int h=1;
    cin>>s1;
    lqq[s1]=h++;
	for(int i=0;i<n;i++){
		string s1,s2,s3;
		cin>>s1>>s2>>s3;
		if(!lqq[s1])lqq[s1]=h++;
		if(!lqq[s2])lqq[s2]=h++;
		if(!lqq[s3])lqq[s3]=h++;
	//	cout<<h<<endl;
		a[lqq[s3]]++,a[lqq[s2]]++;
		s[lqq[s1]].push_back(lqq[s3]);
		s[lqq[s3]].push_back(lqq[s2]);
	}
//	for(auto j:lqq)cout<<j.first<<" "<<j.second<<endl;
	for(int i=1;i<h;i++){
	//	cout<<a[i]<<endl;
		if(!a[i]){
			cout<<i<<endl;
			dfs(i,1);
		}
	}
	cout<<ans<<endl;
}
